﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MDM.GUI.MDM.DataObjects;
using System.Collections;
using System.Xml;
using System.Windows.Forms;
using System.IO;

namespace MDM.GUI.MDM.DataAccess
{
    class ModuleIO
    {

        public static void LoadModuleList()
        {
         
           /* Hashtable ModuleList = new Hashtable();
            XmlDataDocument doc = new XmlDataDocument();
            ModuleInfor itemModule;
            doc.Load(Application.StartupPath + "//installed.xml");
            try
            {
                foreach (XmlNode item in doc.GetElementsByTagName("Module"))
                {
                    itemModule = new ModuleInfor(item.Attributes["Name"].Value,
                                                 item.Attributes["Author"].Value,
                                                 item.Attributes["DateInstall"].Value,
                                                 item.Attributes["Size"].Value,
                                                 item.Attributes["Version"].Value);

                    Common.aModInfor.Add(itemModule);
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Loi: " + e.Message);
            }*/


            MDM.DataObjects.Installed itemModule = AQFramework.Utilities.Serializer<MDM.DataObjects.Installed>.Load(Common.sModuleDestinationPath + "\\installed.xml");
            ModuleInfor itemModules;
            foreach(MDM.DataObjects.Module item in itemModule.Modules)
            {
                
                itemModules = new ModuleInfor(item.Name, item.DateInstall, item.Size, item.Version);
                Common.aModInfor.Add(itemModules);
            }

           



        }





        public static void UpdateNewModule()
        {
            MDM.DataObjects.Installed itemModule = AQFramework.Utilities.Serializer<MDM.DataObjects.Installed>.Load(Common.sModuleDestinationPath + "\\installed.xml");

            MDM.DataObjects.Version itemVersion = AQFramework.Utilities.Serializer<MDM.DataObjects.Version>.Load(Common.sModulePath + "\\" + Common.sVersionXmlName);

            string sGetName = "";
            foreach (MDM.DataObjects.File itemVer in itemVersion.Installs.Files)
            {
                // MDM.DataObjects.Module abc = new MDM.DataObjects.Module();


                MDM.DataObjects.Module abc = new MDM.DataObjects.Module();
                bool bltest = itemVer.Name.EndsWith("exe");

                if (bltest == true)
                {

                    for (int i = itemVer.Name.Length - 1; i >= 0; i = i - 1)
                    {

                        if (itemVer.Name[i] == '.')
                        {
                            sGetName = itemVer.Name.Substring(0, i);

                            break;
                        }

                    }
                    abc.Name = sGetName;
                    abc.Size = itemVer.Size;
                    abc.DateInstall = DateTime.Today.ToString("dd/MM/yyyy");
                    abc.Version = itemVersion.version;
                    abc.VersionID = Common.sVersionID;
                    abc.Images =itemVersion.Images.Path;

                    itemModule.Modules.Add(abc);
                    break;
                }
            }

            AQFramework.Utilities.Serializer<MDM.DataObjects.Installed>.Save(itemModule, Common.sModuleDestinationPath + "\\installed.xml");
            FileInfo fiModule = new FileInfo(Common.sModuleDestinationPath + "\\installed.xml");

        }
        
      
        public static void LoadVersionXML( String sLocation) 
        {
    //        XmlDataDocument doc = new XmlDataDocument();
            // doing later


        }

    }
}
